package com.chagee.store.operation.server.generator.codeGenerator;


import com.chagee.store.operation.common.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: hk
 * @date: 2024/05/20
 */
@Service
public class SettlementNoCodeGenerator extends AbstractCodeGenerator {

	private static final String KEY_PREFIX = "JS:NO:CODE";

	private static final String VALUE_PREFIX = "JS";

	private static final Integer NUM_LENGTH = 5;

	private static final Integer PLACE_HOLDER_NUM = NumberUtils.INTEGER_ZERO;

	private static final Duration EXPIRE = Duration.ofDays(NumberUtils.INTEGER_ONE);

	@Resource
	protected RedisTemplate<String, Object> redisTemplate;

	@Override
	public List<String> generate(int quantity) {
		String dateStr = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
		String redisKey = CommonUtil.getRedisKey(KEY_PREFIX, dateStr);
		List<String> nums = generateSerial(redisTemplate, redisKey, EXPIRE, quantity);
		return nums.stream().map(t -> VALUE_PREFIX + dateStr + t).collect(Collectors.toList());
	}

	@Override
	protected String fillSerial(long num) {
		return StringUtils.leftPad(String.valueOf(num), NUM_LENGTH, String.valueOf(PLACE_HOLDER_NUM));
	}

}
